# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def kthSmallest(self, root: TreeNode, k: int) -> int:
        self.count, self.k = 0, k
        try:
            self.search(root)
        except:
            return self.res

    def search(self, root: TreeNode):
        if root.left:
            self.search(root.left)
        self.count += 1
        if self.count == self.k:
            self.res = root.val
            raise StopAsyncIteration
        if root.right:
            self.search(root.right)
